package com.course_design.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.course_design.entity.Class;
import com.course_design.entity.Student;
import com.course_design.entity.Teacher;
import com.course_design.service.ClassService;
import com.course_design.service.StudentService;
import com.course_design.util.TeacherExcelUtil;

@Controller
public class ClassController {
	@Autowired
	private ClassService classService;
	
	@Autowired
	private StudentService studentService;
	@RequestMapping(value="/queryClassBySchoolId",method=RequestMethod.GET)
	@ResponseBody
	public Msg queryClassBySchoolId(@RequestParam("schoolId")Integer schoolId,HttpServletRequest request) {
		HttpSession session=request.getSession();
		Teacher teacher=(Teacher)session.getAttribute("teacher");
		if(teacher!=null) {
			List<Class> classes=classService.queryClassBySchoolId(schoolId, teacher.getTeacherid());
			return Msg.success("OK").add("classInfo",classes);
		}
		return null;
	}
	@RequestMapping(value="/addClass",method=RequestMethod.POST)
	@ResponseBody
	public Msg queryClassBySchoolId(@RequestParam(value="className")String className,@RequestParam(value="schoolId")Integer schoolId,
			HttpServletRequest request,@RequestParam(value="file",required=false)MultipartFile file) {
		System.out.println(file);
		if(className.equals("")&&schoolId==null) {
			return Msg.error("Error");
		}
		HttpSession session=request.getSession();
		Teacher teacher=(Teacher)session.getAttribute("teacher");
		if(teacher==null) {
			return Msg.error("noLogin");
		}
		Class class1=new Class();
		class1.setClassname(className);
		class1.setSchool(schoolId);
		class1.setCreatedteacher(teacher.getTeacherid());
		//插入班级
		class1.setClassid(classService.addClass(class1));
		
		String originalFileName=file.getOriginalFilename();
		String suffixName=originalFileName.substring(originalFileName.indexOf("."));
		File ioFile=new File("/fileupload/temp");
		if(!ioFile.exists()) {
			 //使文件可以改，因为Tomcat发布服务后，文件的权限不一定是可以改的
			ioFile.setWritable(true);
            //使用dirs是为了解决上传的路径中，如果有文件夹的没有创建，其会自动创建文件夹
			ioFile.mkdirs();
		}
		
		List<Student> students=new ArrayList<>();
		File targetFile=new File("/fileupload/temp",file.getOriginalFilename());
		try {
			file.transferTo(targetFile);
			TeacherExcelUtil util=new TeacherExcelUtil();
			
			if(suffixName.equals(".xlsx")) {
				students=util.readXlsx(targetFile, class1.getClassid());
				
			}else {
				students=util.readXls(targetFile, class1.getClassid());
			}
			studentService.batch(students);
			targetFile.delete();
			return Msg.success("OK");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return Msg.error("error");
	}
	
}
